from typing import List
class Solution:
    def isValid(self,nums,start,nonZeros,direction):
        temp = nums[:]
        curr = start
        while nonZeros>0 and curr>=0 and curr<len(nums):
            if temp[curr] > 0:
                temp[curr] -= 1
                direction*=(-1)
                if temp[curr] == 0:
                    nonZeros -= 1
            curr += direction  
        return nonZeros == 0

        
    def countValidSelections(self, nums: List[int]) -> int: 
        nonZeros = sum(1 if x != 0 else 0 for x in nums)
        cnt = 0
        n = len(nums)
        for i in range(n):
            if nums[i] == 0:
                if self.isValid(nums,i,nonZeros,1):
                    cnt += 1
                if self.isValid(nums,i,nonZeros,-1):
                    cnt += 1
        return cnt